package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.model.TangDBUtil;

/**
 * Servlet implementation class select
 */
@WebServlet("/files/select")
public class select extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public select() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//System.out.println("收到Ajax");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String typeString=request.getParameter("type");
		String textString = request.getParameter("shuru");
		String isUse = request.getParameter("isUse");
		
		PrintWriter out = response.getWriter();
		String[] param ={"%"+textString+"%","yes"}; 
		int[] which = new int[2];
		String countsqlString="";
		String rssqlString="";
		if(typeString.equals("设备名称")){
			countsqlString= "select count(*) as B from device where type like ? and state=? and isUse='"+isUse+"'";
			rssqlString="select * from device where type like ? and state=? and isUse='"+isUse+"'";
			which[0]=0;
			which[1]=0;
		}
		if(typeString.equals("设备编号")){
			countsqlString= "select count(*) as B from device where serial like ? and state=? and isUse='"+isUse+"'";
			rssqlString="select * from device where serial like ? and state=? and isUse='"+isUse+"'";
			which[0]=0;
			which[1]=0;
		}
		int requestPage = Integer.parseInt(request.getParameter("requestPage"));        //得到当前页面是第几页
		try{
			TangDBUtil connectDB = new TangDBUtil();
			ResultSet rstotalRecord = connectDB.executeQuery(countsqlString,param,2,which);
			int totalrecord=0;
			int totalPage=0;     //获取总共的页数
		    if(rstotalRecord.next()){
		  	  totalrecord=rstotalRecord.getInt("B");
		  	  totalPage = totalrecord/10+1;
		    }
		    ResultSet rsdev = connectDB.executeQuery(rssqlString,param,2,which);
		    int i=0;
		    if(requestPage!=1){
		    	while(rsdev.next()){
			    	i++;
			    	if(i>((requestPage-1)*9-1))
			    		break;
				}
		    }
		    
		    //Json赋值
		    int k =0;
		    JSONObject json = new JSONObject();  
	        JSONArray array = new JSONArray();
	        JSONObject member = null;  
		    while(rsdev.next()){
		    	k++;
		    	if(k>9){
		    		break;
		    	}
		    	member = new JSONObject();  
		    	member.put("name", rsdev.getString("type"));
	            member.put("devid", rsdev.getString("serial"));
	            member.put("devmodel", rsdev.getString("model"));
	            member.put("depart", rsdev.getString("department"));
	            member.put("did", rsdev.getString("did"));
	            member.put("endDate", rsdev.getString("edate"));
	            array.add(member);  
		    }
		    member = new JSONObject();
    		member.put("totalSet", totalrecord);
            member.put("totalPage", totalPage);  
            member.put("curPage",requestPage);      //发送的第几页
            array.add(member);
		    json.put("dev", array);
		    out.print(json.toString());
		    //System.out.println(json.toString());
		    connectDB.closeConnection();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
